/* Copyright 2011 Taobao.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#ifndef _REPSYNC_H_
#define _REPSYNC_H_


#include <ctype.h>
#include <signal.h>
#include <arpa/inet.h>  //htonl & ntohl
#include <dirent.h>
#include <errno.h>

#include <my_global.h>
#include <mysql_com.h>
#include <my_dir.h>
#include <my_sys.h>
#include <my_base.h>


void error_msg(const char * fmt, ...);


#include <mysql.h>
#include <hash.h>
#include <m_string.h>
#include <mysql_time.h>
#include <mysql_com.h>
#include <sql_common.h>

#include "rpl_tblmap.h"
#include "rpl_utility.h"
#include "sql_string.h"
#include "sql_list.h"

#include "config.h"
#include "log_event.h"

FILE *fp_stdout;
const char *LOCAL_BASE_DIR;

#define DEBUG my_printf
#define BIN_LOG_HEADER_SIZE	4
#define PROBE_HEADER_LEN	(EVENT_LEN_OFFSET+4)
#define POSFILE_POS_SIZE 4
#define LOG_POS_INIT     4
#define POSFILE_STR_LEN_SIZE 4
#define INT64_LEN   (sizeof("-9223372036854775808") - 1)
#define THREAD_STACK_SIZE (1024 * 1024)  /* 1M */
#define MAX_STRING_LEN      1024
#define EVENT_INTERVAL      1000

#define MAX_PK      10
/////////////////////////////////
//
typedef struct {
    int head;
    int tail;
    int loops;
} node_t;

typedef struct {
    char *upp_table;
    char *low_table;
    char *upp_pk;
    char *low_pk;
    unsigned int valid;
} pk_map_t;

#include<semaphore.h>

node_t *node;
sem_t *sems_full;
sem_t *sems_empty;
char ***buffer;
#define SLOT_ITEMS      16
#define ITEM_LEN        (1024 * 4)
#define N_STATS_ITV     1000



enum Exit_status {
	OK_CONTINUE= 0,
	DEBUG_STOP,
	OK_STOP
};

typedef struct {
	const char *pidfile;
	const char *logfile;
	const char *base_dir;
    const char *cnf_file;
    const char *ignore;
    const char *autoinc_id;
    int pattern;
    int save_binlog;
    int daemon;
    int autocommit;
    int dump_diff;
    int force_group;
    int forward_slave;
    int part_binlog;
    int apply_binlog;
    int n_master;
    int n_slave;
    int debug_sql;
    int running;
    int test;

}setting_st;

typedef struct mysql_info_st {
	const char* host;
	const char* user;
    const char *database;
    const char *table;
	const char* password;
	const char* name;
	int port;
	int server_id;
} mysql_info_st;

typedef struct master_info_st {
	pthread_t tid;
	mysql_info_st *mysql_info;
    MYSQL mysql;
    int index;
    int slave_index;
    int group_index;
    int tid_index;
    int group_head_index;
    int n_instance;
	const char* binlog_cur;
	const char* binlog_start;
    unsigned long binlog_pos;
	int fd;					
	int pos_fd;
    FILE *dump_fd;
	ulonglong trace_log_pos_offset;
    int trace_log_pos_events;
	Format_description_log_event* description_event;
	size_t stats_binlog_write;  /* the counter for binlog write */
} master_info_st;

typedef struct slave_info_st {
	pthread_t *tids;
    int n_slides;
    mysql_info_st *mysql_info;
    MYSQL *mysql;
    int n_master;
} slave_info_st;


#endif
